// components/receive/commonList/commonList.js
import Dialog from "../../../miniprogram_npm/@vant/weapp/dialog/dialog";
import api from '../../../utils/api';
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    canEdit: false,
    customerId: '', //客户ID
    claimId: '', //认领id
    invoiceList: {
      type: Array,
      value: []
    }, // 发票
    contractList: {
      type: Array,
      value: []
    }, // 合同
    taskList: {
      type: Array,
      value: []
    }, // 任务号

  },

  /**
   * 组件的初始数据
   */
  data: {
    rwidth: 80
  },
  lifetimes: {
    attached() {}
  },
  /**
   * 组件的方法列表
   */
  methods: {
    
    // 删除发票
    delInvoice(id) {
      let that = this;
      api.claimCancelInvoice({
        data: {
          invoiceStr: id,
          claimId: that.data.claimId
        },
        success: function (res) {
          if (res.data.code == 200) {
            wx.showToast({
              title: '删除成功',
            })
            that.triggerEvent('myevent', 'del')
            // let invoiceList = []
            // for (let i = 0; i < that.data.invoiceList.length; i++) {
            //   if (id != that.data.invoiceList[i].id) {
            //     invoiceList.push(that.data.invoiceList[i])
            //   }
            // }
            // that.setData({
            //   invoiceList: invoiceList
            // })
          } else {
            wx.showModal({
              content: res.data.message,
              showCancel: false,
              confirmColor: '#0041C4'
            })
          }
        }
      })
    },
    // 删除合同
    delContract(id) {
      let that = this;
      api.claimCancelContract({
        data: {
          contractStr: id,
          claimId: that.data.claimId
        },
        success: function (res) {
          if (res.data.code == 200) {
            wx.showToast({
              title: '删除成功',
            })
            that.triggerEvent('myevent', 'del')
            // let contractList = []
            // for (let i = 0; i < that.data.contractList.length; i++) {
            //   if (id != that.data.contractList[i].id) {
            //     contractList.push(that.data.contractList[i])
            //   }
            // }
            // that.setData({
            //   contractList: contractList
            // })
          } else {
            wx.showModal({
              content: res.data.message,
              showCancel: false,
              confirmColor: '#0041C4'
            })
          }
        }
      })
    },
    // 删除任务号
    delTask(id) {
      let that = this;
      api.claimCancelTask({
        data: {
          taskStr: id,
          claimId: that.data.claimId
        },
        success: function (res) {
          if (res.data.code == 200) {
            wx.showToast({
              title: '删除成功',
            })
            that.triggerEvent('myevent', 'del')
            // let taskList = []
            // for (let i = 0; i < that.data.taskList.length; i++) {
            //   if (id != that.data.taskList[i].id) {
            //     taskList.push(that.data.taskList[i])
            //   }
            // }
            // that.setData({
            //   taskList: taskList
            // })
          } else {
            wx.showModal({
              content: res.data.message,
              showCancel: false,
              confirmColor: '#0041C4'
            })
          }
        }
      })
    },
    // 删除
    onClose(event) {
      console.log(event)
      let that = this;
      const {
        position,
        instance
      } = event.detail;
      const {
        delid,
        deltype
      } = event.currentTarget.dataset;
      switch (position) {
        case 'right':
          Dialog.confirm({
            context: this,
            selector: "#van-dialog",
            message: '确定删除吗？',
          }).then(() => {
            if (deltype == 0) {
              that.delInvoice(delid)
            } else if (deltype == 1) {
              that.delContract(delid)
            } else if (deltype == 2) {
              that.delTask(delid)
            }
            
            instance.close();
          }).catch(() => {
            instance.close();
          });
          break;
      }
    },
    // 详情
    goDetail(e) {
      console.log(e)
      let that = this;
      let canEdit = that.data.canEdit ? 1 : 0
      switch (e.currentTarget.dataset.type) {
        case '0':
          wx.navigateTo({
            url: '/pages/receive/editBill/editBill?claimInvoiceId=' + e.currentTarget.dataset.id + '&canEdit=' + canEdit,
          });
          break;
        case '1':
          wx.navigateTo({
            url: '/pages/receive/editCcontract/editCcontract?claimContractId=' + e.currentTarget.dataset.id + '&canEdit=' + canEdit,
          });
          break;
        case '2':
          wx.navigateTo({
            url: '/pages/receive/editTaskNumber/editTaskNumber?claimTaskId=' + e.currentTarget.dataset.id + '&canEdit=' + canEdit,
          });
          break;
        case '3':
          wx.navigateTo({
            url: '/pages/receive/addInvoice/addInvoice?claimId=' + that.data.claimId + '&customerId=' + that.data.customerId,
          });
          break;
        case '4':
          wx.navigateTo({
            url: '/pages/receive/addContract/addContract?claimId=' + that.data.claimId + '&customerId=' + that.data.customerId,
          });
          break;
        case '5':
          wx.navigateTo({
            url: '/pages/receive/addTask/addTask?claimId=' + that.data.claimId + '&customerId=' + that.data.customerId,
          });
          break;
      }


    },

  }
})